//
//  AppDelegate.swift
//  TKWBOne
//
//  Created by tiankun on 16/8/22.
//  Copyright © 2016年 1. All rights reserved.
// App Key：2998868377
// App Secret：0d953d99dab04c12401446d43ec4ea65
// token: 2.00OBxHPEnewwQD8a104a0ff4SgbfuD

import UIKit
import UserNotifications
import AFNetworking
import SVProgressHUD

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        setupAdditions()
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = TKMainViewController()
        window?.makeKeyAndVisible()
        loadAppInfo()
        return true
    }
}

extension AppDelegate {
    private func setupAdditions() {
        //设置SVProgressHUD最小解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        //设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        //设置用户授权通知
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .carPlay, .sound]) { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            }
        } else {
            // Fallback on earlier versions
            let notificationSet = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notificationSet)
        }
    }
}
//MARK:从服务器加载应用程序信息
extension AppDelegate {
    private func loadAppInfo() {
        //模拟异步
        DispatchQueue.global().async {
            //url
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            //data
            let data = NSData(contentsOf: url!)
            //写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let path = (docDir as NSString).appendingPathComponent("main.json")
            data?.write(toFile: path, atomically: true)
            
        }
    }
}
